package com.zimi.core.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
@Slf4j
public class AppExceptionHandler {


    @ExceptionHandler(Exception.class)
    public Object exceptionHandler(HttpServletRequest request, Exception e) {
        log.error(String.valueOf(e));
        Map<String, Object> attr = new HashMap<>();
        String str = "500";
        if (null != request.getAttribute("javax.servlet.error.status_code"))
            str = request.getAttribute("javax.servlet.error.status_code").toString();
        attr.put("url", request.getRequestURL());
        attr.put("code", Integer.parseInt(str));
        attr.put("msg", e.getMessage());
        return attr;
    }
}
